
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Emberjs,Ember-Teach,Ember-Model," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>自定义序列化器 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        自定义序列化器
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:37.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Model/" rel="tag">Ember-Model</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Teach/" rel="tag">Ember-Teach</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Emberjs/" rel="tag">Emberjs</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 9 分钟</span>
    </header>

    <div class="post-content">
      <p>在<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>应用中，序列化器会格式化与后台交互的数据，包括发送和接收的数据。默认情况下会使用<a target="_blank" rel="noopener" href="http://jsonapi.org/">JSON API</a>序列化数据。如果你的后端使用不同的格式，Ember Data允许你自定义序列化器或者定义一个完全不同的序列化器。</p>
<p>Ember Data内置了三个序列化器。<a target="_blank" rel="noopener" href="http://emberjs.com/api/data/classes/DS.JSONAPISerializer.html">JSONAPISerializer</a>是默认的序列化器，用与处理后端的JSON API。<a target="_blank" rel="noopener" href="http://emberjs.com/api/data/classes/DS.JSONSerializer.html">JSONSerializer</a>是一个简单的序列化器，用与处理单个JSON对象或者是处理记录数组。<a target="_blank" rel="noopener" href="http://emberjs.com/api/data/classes/DS.RESTSerializer.html">RESTSerializer</a>是一个复杂的序列化器，支持侧面加载，在Ember Data2.0之前是默认的序列化器。</p>
<h2 id="JSONAPISerializer规范"><a href="#JSONAPISerializer规范" class="headerlink" title="JSONAPISerializer规范"></a>JSONAPISerializer规范</h2><p>当你向服务器请求数据时，JSONSerializer会把服务器返回的数据当做是符合下列规范的JSON数据。</p>
<p><strong>注意</strong>：特别是项目使用的是自定义适配器的时候，后台返回的数据格式必须符合<a target="_blank" rel="noopener" href="http://www.jsonapi.org/">JSOP API</a>规范，否则无法实现数据的CRUD操作，Ember就无法解析数据，关于自定义适配器这点的知识请看上一篇<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/04/17/zi-ding-yi-gua-pei-qi/">Ember.js 入门指南之四十四自定义适配器</a>，在文章中有详细的介绍自定义适配器和自定义序列化器是息息相关的。</p>
<h4 id="1，JSON-API文档"><a href="#1，JSON-API文档" class="headerlink" title="1，JSON API文档"></a>1，JSON API文档</h4><p>JSONSerializer期待后台返回的是一个符合JSON API规范和约定的JSON文档。比如下面的JSON数据，这些数据的格式是这样的：</p>
<ol>
<li>type指定model的名称</li>
<li>属性名称使用中划线分隔</li>
</ol>
<p>比如请求<code>/people/123</code>，响应的数据如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;data&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;type&quot;</span>: <span class="string">&quot;people&quot;</span>,</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: <span class="string">&quot;123&quot;</span>,</span><br><span class="line">    <span class="string">&quot;attributes&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;first-name&quot;</span>: <span class="string">&quot;Jeff&quot;</span>,</span><br><span class="line">      <span class="string">&quot;last-name&quot;</span>: <span class="string">&quot;Atwood&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如果响应的数据有多条，那么<code>data</code>将是以数组形式返回。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;data&quot;</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">&quot;type&quot;</span>: <span class="string">&quot;people&quot;</span>,</span><br><span class="line">      <span class="string">&quot;id&quot;</span>: <span class="string">&quot;123&quot;</span>,</span><br><span class="line">      <span class="string">&quot;attributes&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;first-name&quot;</span>: <span class="string">&quot;Jeff&quot;</span>,</span><br><span class="line">        <span class="string">&quot;last-name&quot;</span>: <span class="string">&quot;Atwood&quot;</span></span><br><span class="line">      &#125;</span><br><span class="line">  &#125;,&#123;</span><br><span class="line">      <span class="string">&quot;type&quot;</span>: <span class="string">&quot;people&quot;</span>,</span><br><span class="line">      <span class="string">&quot;id&quot;</span>: <span class="string">&quot;124&quot;</span>,</span><br><span class="line">      <span class="string">&quot;attributes&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;first-name&quot;</span>: <span class="string">&quot;chen&quot;</span>,</span><br><span class="line">        <span class="string">&quot;last-name&quot;</span>: <span class="string">&quot;ubuntuvim&quot;</span></span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="2，拷贝数据"><a href="#2，拷贝数据" class="headerlink" title="2，拷贝数据"></a>2，拷贝数据</h4><p>数据有时候并不是请求的主体，如果数据有链接。链接的关系会放在<code>included</code>下面。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;data&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;type&quot;</span>: <span class="string">&quot;articles&quot;</span>,</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line">    <span class="string">&quot;attributes&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;title&quot;</span>: <span class="string">&quot;JSON API paints my bikeshed!&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;links&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;self&quot;</span>: <span class="string">&quot;http://example.com/articles/1&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;relationships&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;comments&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;data&quot;</span>: [</span><br><span class="line">          &#123; <span class="string">&quot;type&quot;</span>: <span class="string">&quot;comments&quot;</span>, <span class="string">&quot;id&quot;</span>: <span class="string">&quot;5&quot;</span> &#125;,</span><br><span class="line">          &#123; <span class="string">&quot;type&quot;</span>: <span class="string">&quot;comments&quot;</span>, <span class="string">&quot;id&quot;</span>: <span class="string">&quot;12&quot;</span> &#125;</span><br><span class="line">        ]</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;],</span><br><span class="line">  <span class="string">&quot;included&quot;</span>: [&#123;</span><br><span class="line">    <span class="string">&quot;type&quot;</span>: <span class="string">&quot;comments&quot;</span>,</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: <span class="string">&quot;5&quot;</span>,</span><br><span class="line">    <span class="string">&quot;attributes&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;body&quot;</span>: <span class="string">&quot;First!&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;links&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;self&quot;</span>: <span class="string">&quot;http://example.com/comments/5&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;, &#123;</span><br><span class="line">    <span class="string">&quot;type&quot;</span>: <span class="string">&quot;comments&quot;</span>,</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: <span class="string">&quot;12&quot;</span>,</span><br><span class="line">    <span class="string">&quot;attributes&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;body&quot;</span>: <span class="string">&quot;I like XML better&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;links&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;self&quot;</span>: <span class="string">&quot;http://example.com/comments/12&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>从JSON数据看出，<code>id</code>为<code>5</code>的<code>comment</code>链接是<code>&quot;self&quot;: http://example.com/comments/5</code>。<code>id</code>为<code>12</code>的<code>comment</code>链接是<code>&quot;self&quot;: http://example.com/comments/12</code>。并且这些链接是单独放置<code>included</code>内。</p>
<h4 id="3，自定义序列化器"><a href="#3，自定义序列化器" class="headerlink" title="3，自定义序列化器"></a>3，自定义序列化器</h4><p>Ember Data默认的序列化器是JSONAPISerializer，但是你也可以自定义序列化器覆盖默认的序列化器。</p>
<p>要自定义序列化器首先要定义一个名为<code>application</code>序列化器作为入口。</p>
<p>直接使用命令生成：<code>ember g serializer application</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/serializers/application.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> DS <span class="keyword">from</span> <span class="string">&#x27;ember-data&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.JSONSerializer.extend(&#123;</span><br><span class="line"></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>甚至你还可以针对某个模型定义序列化器。比如下面的代码为<code>post</code>定义了一个专门的序列化器，在前一篇自定义适配器中介绍过如何为一个模型自定义适配器，这个两个是相关的。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/serializers/post.js</span></span><br><span class="line"><span class="keyword">import</span> DS <span class="keyword">from</span> ‘ember-data’;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.JSONSerializer.extend(&#123;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>如果你想改变发送到后端的JSON数据格式，你只需重写<code>serialize</code>回调，在回调中设置数据格式。</p>
<p>比如前端发送的数据格式是如下结构， </p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;data&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;attributes&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line">      <span class="string">&quot;name&quot;</span>: <span class="string">&quot;My Product&quot;</span>,</span><br><span class="line">      <span class="string">&quot;amount&quot;</span>: <span class="number">100</span>,</span><br><span class="line">      <span class="string">&quot;currency&quot;</span>: <span class="string">&quot;SEK&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;type&quot;</span>: <span class="string">&quot;product&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>但是服务器接受的数据结构是下面这种结构：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;data&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;attributes&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line">      <span class="string">&quot;name&quot;</span>: <span class="string">&quot;My Product&quot;</span>,</span><br><span class="line">      <span class="string">&quot;cost&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;amount&quot;</span>: <span class="number">100</span>,</span><br><span class="line">        <span class="string">&quot;currency&quot;</span>: <span class="string">&quot;SEK&quot;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;type&quot;</span>: <span class="string">&quot;product&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此时你可以重写<code>serialize</code>回调。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/serializers/application.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> DS <span class="keyword">from</span> <span class="string">&#x27;ember-data&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.JSONSerializer.extend(&#123;</span><br><span class="line">  serialize: <span class="function"><span class="keyword">function</span>(<span class="params">snapshot, options</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> json = <span class="built_in">this</span>._super(...arguments);  <span class="comment">// ??</span></span><br><span class="line">    json.data.attributes.cost = &#123;</span><br><span class="line">      amount: json.data.attributes.amount,</span><br><span class="line">      currency: json.data.attributes.currency</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">delete</span> json.data.attributes.amount;</span><br><span class="line">    <span class="keyword">delete</span> json.data.attributes.currency;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> json;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>那么如果是反过来呢。<br>如果后端返回的数据格式为：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;data&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;attributes&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line">      <span class="string">&quot;name&quot;</span>: <span class="string">&quot;My Product&quot;</span>,</span><br><span class="line">      <span class="string">&quot;cost&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;amount&quot;</span>: <span class="number">100</span>,</span><br><span class="line">        <span class="string">&quot;currency&quot;</span>: <span class="string">&quot;SEK&quot;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;type&quot;</span>: <span class="string">&quot;product&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>但是前端需要的格式是：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;data&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;attributes&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line">      <span class="string">&quot;name&quot;</span>: <span class="string">&quot;My Product&quot;</span>,</span><br><span class="line">      <span class="string">&quot;amount&quot;</span>: <span class="number">100</span>,</span><br><span class="line">      <span class="string">&quot;currency&quot;</span>: <span class="string">&quot;SEK&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;type&quot;</span>: <span class="string">&quot;product&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此时你可以重写回调方法<code>normalizeResponse</code>或<code>normalize</code>，在方法里设置数据格式：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/serializers/application.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> DS <span class="keyword">from</span> <span class="string">&#x27;ember-data&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.JSONSerializer.extend(&#123;</span><br><span class="line"></span><br><span class="line">  normalizeResponse: <span class="function"><span class="keyword">function</span>(<span class="params">store, primaryModelClass, payload, id, requestType</span>) </span>&#123;</span><br><span class="line">    payload.data.attributes.amount = payload.data.attributes.cost.amount;</span><br><span class="line">    payload.data.attributes.currency = payload.data.attributes.cost.currency;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">delete</span> payload.data.attributes.cost;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>._super(...arguments);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>####4，数据ID属性</p>
<p>每一条数据都有一个唯一值作为<code>ID</code>，默认情况下Ember会为每个模型加上一个名为<code>id</code>的属性。如果你想改为其他名称，你可以在序列化器中指定。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/serializers/application.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> DS <span class="keyword">from</span> <span class="string">&#x27;ember-data&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.JSONSerializer.extend(&#123;</span><br><span class="line">  primatyKey: <span class="string">&#x27;__id&#x27;</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>把数据主键名修改为<code>__id</code>。</p>
<h4 id="5，属性名"><a href="#5，属性名" class="headerlink" title="5，属性名"></a>5，属性名</h4><p>Ember Data约定的属性名是驼峰式的命名方式，但是序列化器却期望的是中划线分隔的命名方式，不过Ember会自动转换，不需要开发者手动指定。然而，如果你想修改这种默认的方式也是可以的，只需在序列化器中使用属性<code>keyForAttributes</code>指定你喜欢的分隔方式即可。比如下面的代码把序列号的属性名称改为以下划线分隔：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/serializers/application.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> DS <span class="keyword">from</span> <span class="string">&#x27;ember-data&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.JSONSerializer.extend(&#123;</span><br><span class="line">  keyForAttributes: <span class="function"><span class="keyword">function</span>(<span class="params">attr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> Ember.String.underscore(attr);  </span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h4 id="6，指定属性名的别名"><a href="#6，指定属性名的别名" class="headerlink" title="6，指定属性名的别名"></a>6，指定属性名的别名</h4><p>如果你想模型数据被序列化、反序列化时指定模型属性的别名，直接在序列化器中使用<code>attrs</code>属性指定即可。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/models/person.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.Model.extend(&#123;</span><br><span class="line">  lastName: DS.attr(‘string’)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>指定序列化、反序列化属性别名：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/serializers/application.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> DS <span class="keyword">from</span> <span class="string">&#x27;ember-data&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.JSONSerializer.extend(&#123;</span><br><span class="line">  attrs: &#123;</span><br><span class="line">    lastName: ‘lastNameOfPerson’</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>指定模型属性别名为<code>lastNameOfPerson</code>。</p>
<h4 id="7，模型之间的关联关系"><a href="#7，模型之间的关联关系" class="headerlink" title="7，模型之间的关联关系"></a>7，模型之间的关联关系</h4><p>一个模型通过<code>ID</code>引用另一个模型。比如有两个模型存在一对多关系：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/models/post.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.Model.extend(&#123;</span><br><span class="line">  comments: DS.hasMany(‘comment’, &#123; <span class="attr">async</span>: <span class="literal">true</span> &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>序列化后JSON数据格式如下，其中关联关系通过一个存放<code>ID</code>属性值的数组实现。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;data&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;type&quot;</span>: <span class="string">&quot;posts&quot;</span>,</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line">    <span class="string">&quot;relationships&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;comments&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;data&quot;</span>: [</span><br><span class="line">          &#123; <span class="string">&quot;type&quot;</span>: <span class="string">&quot;comments&quot;</span>, <span class="string">&quot;id&quot;</span>: <span class="string">&quot;5&quot;</span> &#125;,</span><br><span class="line">          &#123; <span class="string">&quot;type&quot;</span>: <span class="string">&quot;comments&quot;</span>, <span class="string">&quot;id&quot;</span>: <span class="string">&quot;12&quot;</span> &#125;</span><br><span class="line">        ]</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可见，有两个<code>comment</code>关联到一个<code>post</code>上。<br>如果是<code>belongsTo</code>关系的，JSON结构与<code>hadMany</code>关系相差不大。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;data&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;type&quot;</span>: <span class="string">&quot;comment&quot;</span>,</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line">    <span class="string">&quot;relationships&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;original-post&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;data&quot;</span>: &#123; <span class="string">&quot;type&quot;</span>: <span class="string">&quot;post&quot;</span>, <span class="string">&quot;id&quot;</span>: <span class="string">&quot;5&quot;</span> &#125;,</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>id</code>为<code>1</code>的<code>comment</code>关联了<code>ID</code>为<code>5</code>的<code>post</code>。</p>
<h4 id="8，自定义转换规则"><a href="#8，自定义转换规则" class="headerlink" title="8，自定义转换规则"></a>8，自定义转换规则</h4><p>在某些情况下，Ember内置的属性类型（<code>string</code>、<code>number</code>、<code>boolean</code>、<code>date</code>）还是不够用的。比如，服务器返回的是非标准的数据格式时。</p>
<p>Ember Data可以注册新的JSON转换器去格式化数据，可用直接使用命令创建：<code>ember g transform coordinate-point</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/transforms/coordinate-point.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> DS <span class="keyword">from</span> <span class="string">&#x27;ember-data&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.Transform.extend(&#123;</span><br><span class="line">  deserialize: <span class="function"><span class="keyword">function</span>(<span class="params">v</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> [v.get(<span class="string">&#x27;x&#x27;</span>), v.get(<span class="string">&#x27;y&#x27;</span>)];</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  serialize: <span class="function"><span class="keyword">function</span>(<span class="params">v</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> Ember.create(&#123; <span class="attr">x</span>: v[<span class="number">0</span>], <span class="attr">y</span>: v[<span class="number">1</span>]&#125;);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>定义一个复合属性类型，这个类型由两个属性构成，形成一个坐标。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/models/curor.js</span></span><br><span class="line"><span class="keyword">import</span> DS <span class="keyword">from</span> <span class="string">&#x27;ember-data&#x27;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.Model.extend(&#123;</span><br><span class="line">  position: DS.attr(‘coordinate-point’)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>自定义的属性类型使用方式与普通类型一致，直接作为<code>attr</code>方法的参数。最后当我们接受到服务返回的数据形如下面的代码所示：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  cursor: &#123;</span><br><span class="line">    position: [<span class="number">4</span>, <span class="number">9</span>]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>加载模型实例时仍然作为一个普通对象加载。仍然可以使用<code>.</code>操作获取属性值。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> cursor = <span class="built_in">this</span>.store.findRecord(‘cursor’, <span class="number">1</span>);</span><br><span class="line">cursor.get(‘position.x’);  <span class="comment">//  =&gt; 4</span></span><br><span class="line">cursor.get(‘position.y’);  <span class="comment">//  =&gt; 9</span></span><br></pre></td></tr></table></figure>
<h4 id="9，JSONSerializer"><a href="#9，JSONSerializer" class="headerlink" title="9，JSONSerializer"></a>9，JSONSerializer</h4><p>并不是所有的API都遵循JSONAPISerializer约定通过数据命名空间和拷贝关系记录。比如系统遗留问题，原先的API返回的只是简单的JSON格式并不是JSONAPISerializer约定的格式，此时你可以自定义序列化器去适配旧接口。并且可以同时兼容使用RESTAdapter去序列号这些简单的JSON数据。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/serializer/application.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.JSONSerializer.extend(&#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h4 id="10，EMBEDDEDRECORDMIXIN"><a href="#10，EMBEDDEDRECORDMIXIN" class="headerlink" title="10，EMBEDDEDRECORDMIXIN"></a>10，EMBEDDEDRECORDMIXIN</h4><p>尽管Ember Data鼓励你拷贝模型关联关系，但有时候在处理遗留API时，你会发现你需要处理的JSON中嵌入了其他模型的关联关系。不过<code>EmbeddedRecordsMixin</code>可以帮你解决这个问题。</p>
<p>比如<code>post</code>中包含了一个<code>author</code>记录。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: <span class="string">&quot;1&quot;</span>,</span><br><span class="line">    <span class="string">&quot;title&quot;</span>: <span class="string">&quot;Rails is omakase&quot;</span>,</span><br><span class="line">    <span class="string">&quot;tag&quot;</span>: <span class="string">&quot;rails&quot;</span>,</span><br><span class="line">    <span class="string">&quot;authors&quot;</span>: [</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;id&quot;</span>: <span class="string">&quot;2&quot;</span>,</span><br><span class="line">            <span class="string">&quot;name&quot;</span>: <span class="string">&quot;Steve&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>你可以定义里的模型关联关系如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/serializers/post.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.JSONSerialier.extend(DS.EmbeddedRecordsMixin, &#123;</span><br><span class="line">  attrs: &#123;</span><br><span class="line">author: &#123;</span><br><span class="line">  serialize: ‘records’,</span><br><span class="line">  deserialize: ‘records’</span><br><span class="line">&#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>如果你发生对象本身需要序列化与反序列化嵌入的关系，你可以使用属性<code>embedded</code>设置。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/serializers/post.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.JSONSerialier.extend(DS.EmbeddedRecordsMixin, &#123;</span><br><span class="line">  attrs: &#123;</span><br><span class="line">author: &#123; <span class="attr">embedded</span>: ‘always’ &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>序列化与反序列化设置有3个关键字：</p>
<ol>
<li><code>records</code>  用于标记全部的记录都是序列化与反序列化的</li>
<li><code>ids</code>  用于标记仅仅序列化与反序列化记录的id</li>
<li><code>false</code>  用于标记记录不需要序列化与反序列化</li>
</ol>
<p>例如，你可能会发现你想读一个嵌入式记录提取时一个JSON有效载荷只包括关系的身份在序列化记录。这可能是使用<code>serialize: ids</code>。你也可以选择通过设置序列化的关系 <code>serialize: false</code>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.JSONSerializer.extend(DS.EmbeddedRecordsMixin, &#123;</span><br><span class="line">  attrs: &#123;</span><br><span class="line">    author: &#123;</span><br><span class="line">      serialize: <span class="literal">false</span>,</span><br><span class="line">      deserialize: <span class="string">&#x27;records&#x27;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    comments: &#123;</span><br><span class="line">      deserialize: <span class="string">&#x27;records&#x27;</span>,</span><br><span class="line">      serialize: <span class="string">&#x27;ids&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h4 id="11，EMBEDDEDRECORDSMIXIN-默认设置"><a href="#11，EMBEDDEDRECORDSMIXIN-默认设置" class="headerlink" title="11，EMBEDDEDRECORDSMIXIN 默认设置"></a>11，EMBEDDEDRECORDSMIXIN 默认设置</h4><p>如果你没有重写<code>attrs</code>去指定模型的关联关系，那么<code>EmbeddedRecordsMixin</code>会有如下的默认行为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">belongsTo：&#123;serialize: ‘id’, deserialize: ‘id’ &#125;</span><br><span class="line">hasMany: &#123; serialize: false, deserialize: ‘ids’ &#125;</span><br></pre></td></tr></table></figure>

<h4 id="12，创作序列化器"><a href="#12，创作序列化器" class="headerlink" title="12，创作序列化器"></a>12，创作序列化器</h4><p>如果项目需要自定义序列化器，Ember推荐扩展JSONAIPSerializer或者JSONSerializer来实现你的需求。但是，如果你想完全创建一个全新的与JSONAIPSerializer、JSONSerializer都不一样的序列化器你可以扩展<code>DS.Serializer</code>类，但是你必须要实现下面三个方法：</p>
<ol>
<li>normalizeResponse</li>
<li>serialize</li>
<li>normalize</li>
</ol>
<p>知道规范化JSON数据对Ember Data来说是非常重要的，如果模型属性名不符合Ember Data规范这些属性值将不会自动更新。如果返回的数据没有在模型中指定那么这些数据将会被忽略。比如下面的模型定义，<code>this.store.push()</code>方法接受的格式为第二段代码所示。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//   app/models/post.js</span></span><br><span class="line"><span class="keyword">import</span> DS <span class="keyword">from</span> <span class="string">&#x27;ember-data&#x27;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.Model.extend(&#123;</span><br><span class="line">  title: DS.attr(‘string’),</span><br><span class="line">  tag: DS.attr(‘string’),</span><br><span class="line">  comments: hasMany(‘comment’, &#123; <span class="attr">async</span>: <span class="literal">true</span> &#125;),</span><br><span class="line">  relatedPosts: hasMany(‘post’)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  data: &#123;</span><br><span class="line">    id: <span class="string">&quot;1&quot;</span>,</span><br><span class="line">    type: <span class="string">&#x27;post&#x27;</span>,</span><br><span class="line">    attributes: &#123;</span><br><span class="line">      title: <span class="string">&quot;Rails is omakase&quot;</span>,</span><br><span class="line">      tag: <span class="string">&quot;rails&quot;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    relationships: &#123;</span><br><span class="line">      comments: &#123;</span><br><span class="line">        data: [&#123; <span class="attr">id</span>: <span class="string">&quot;1&quot;</span>, <span class="attr">type</span>: <span class="string">&#x27;comment&#x27;</span> &#125;,</span><br><span class="line">               &#123; <span class="attr">id</span>: <span class="string">&quot;2&quot;</span>, <span class="attr">type</span>: <span class="string">&#x27;comment&#x27;</span> &#125;],</span><br><span class="line">      &#125;,</span><br><span class="line">      relatedPosts: &#123;</span><br><span class="line">        data: &#123;</span><br><span class="line">          related: <span class="string">&quot;/api/v1/posts/1/related-posts/&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>每个序列化记录必须按照这个格式要正确地转换成Ember Data记录。<br><br><br>本篇的内容难度很大，属于高级主题的内容！如果暂时理解不来不要紧，你可以先使用<a target="_blank" rel="noopener" href="http://www.firebase.com/">firebase</a>构建项目，等你熟悉了整个Ember流程以及数据是如何交互之后再回过头看这篇和上一篇<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/04/17/zi-ding-yi-gua-pei-qi/">Ember.js 入门指南之四十四自定义适配器</a>，这样就不至于难以理解了！！<br><br><br>到本篇为止，有关Ember的基础知识全部介绍完毕！！！从2015-08-26开始到现在刚好2个月，原计划是用3个月时间完成的，提前了一个月，归其原因是后面的内容难度大，理解偏差大！文章质量也不好，感觉时间比较仓促，说以节省了很多时间！（_本篇是重新整理发表的，原始版博文发布的时候Ember还是2.0版本，现在已经是2.5了！！_）<br><br><br>介绍来打算介绍APPLICATION CONCERNS和TESTING这两章！也有可能把旧版的Ember todomvc案例改成Ember2.0版本的，正好可以拿来练练手速！！！<br><br><br>很庆幸的是目标：把旧版的Ember todomvc案例改成Ember2.0版本的，也完成了！！！并且扩展了很多功能，有关代码情况<a target="_blank" rel="noopener" href="https://github.com/ubuntuvim/todos_v2">todos v2</a>，欢迎读者fork学习！如果觉得有用就给我一个<code>star</code>吧！！谢谢！！！</p>
<br>
博文完整代码放在[Github](https://github.com/ubuntuvim/my_emberjs_code)（博文经过多次修改，博文上的代码与github代码可能有出入，不过影响不大！），如果你觉得博文对你有点用，请在github项目上给我点个`star`吧。您的肯定对我来说是最大的动力！！

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/ember-teach/models/%E8%87%AA%E5%AE%9A%E4%B9%89%E9%80%82%E9%85%8D%E5%99%A8/" rel="next" title="自定义适配器">
          自定义适配器
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/ember-teach/models/%E6%9F%A5%E8%AF%A2%E8%AE%B0%E5%BD%95/" rel="prev" title="查询记录">
            查询记录
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#JSONAPISerializer%E8%A7%84%E8%8C%83"><span class="toc-text">JSONAPISerializer规范</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1%EF%BC%8CJSON-API%E6%96%87%E6%A1%A3"><span class="toc-text">1，JSON API文档</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2%EF%BC%8C%E6%8B%B7%E8%B4%9D%E6%95%B0%E6%8D%AE"><span class="toc-text">2，拷贝数据</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3%EF%BC%8C%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BA%8F%E5%88%97%E5%8C%96%E5%99%A8"><span class="toc-text">3，自定义序列化器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5%EF%BC%8C%E5%B1%9E%E6%80%A7%E5%90%8D"><span class="toc-text">5，属性名</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6%EF%BC%8C%E6%8C%87%E5%AE%9A%E5%B1%9E%E6%80%A7%E5%90%8D%E7%9A%84%E5%88%AB%E5%90%8D"><span class="toc-text">6，指定属性名的别名</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7%EF%BC%8C%E6%A8%A1%E5%9E%8B%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E8%81%94%E5%85%B3%E7%B3%BB"><span class="toc-text">7，模型之间的关联关系</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8%EF%BC%8C%E8%87%AA%E5%AE%9A%E4%B9%89%E8%BD%AC%E6%8D%A2%E8%A7%84%E5%88%99"><span class="toc-text">8，自定义转换规则</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#9%EF%BC%8CJSONSerializer"><span class="toc-text">9，JSONSerializer</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10%EF%BC%8CEMBEDDEDRECORDMIXIN"><span class="toc-text">10，EMBEDDEDRECORDMIXIN</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#11%EF%BC%8CEMBEDDEDRECORDSMIXIN-%E9%BB%98%E8%AE%A4%E8%AE%BE%E7%BD%AE"><span class="toc-text">11，EMBEDDEDRECORDSMIXIN 默认设置</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#12%EF%BC%8C%E5%88%9B%E4%BD%9C%E5%BA%8F%E5%88%97%E5%8C%96%E5%99%A8"><span class="toc-text">12，创作序列化器</span></a></li></ol></li></ol></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: '自定义序列化器', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
